package com.afs.electronicforensics.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.afs.electronicforensics.utils.RecordUtils;

//要手动开一下权限,这里没写
public class RecordActivity extends AppCompatActivity {
    private RecordUtils recordUtils;

    private ImageButton record;
    private Chronometer cc;
    private int miss = 0;
    private boolean recordFlag = true;

    private ImageView back;
    private TextView next;


    public static String outputPath;//输出路径

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        this.outputPath = this.getIntent().getStringExtra("RECORD_OUTPUT_PATH");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS},1);
        }

        recordUtils = new RecordUtils(this);

        cc = findViewById(R.id.time);
        record = findViewById(R.id.recorder);

        back = findViewById(R.id.back);
        next = findViewById(R.id.next);

        cc.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                miss++;
                cc.setText(FormatMiss(miss));
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RecordActivity.this.setResult(RESULT_CANCELED);
                RecordActivity.this.finish();
            }
        });
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RecordActivity.this.setResult(RESULT_OK);
                RecordActivity.this.finish();
            }
        });

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(recordFlag){
                    cc.start();
                    record.setBackgroundResource(R.drawable.ic_stop);
                    clickRecorder();
                    recordFlag = false;
                }else{
                    cc.stop();
                    record.setBackgroundResource(R.drawable.ic_start);
                    recordFlag = true;
                    record.setEnabled(false);
                    clickStop();
                    next.setTextColor(Color.BLACK);
                    next.setEnabled(true);
                }

            }
        });
    }

    public static Intent createIntent(Context context, String outputPath) {
        Intent intent = new Intent(context,RecordActivity.class);
        intent.putExtra("RECORD_OUTPUT_PATH", outputPath);
        return intent;
    }




    // 将秒转化成小时分钟秒
    public String FormatMiss(int miss) {
        String hh = miss / 3600 > 9 ? miss / 3600 + "" : "0" + miss / 3600;
        String mm = (miss % 3600) / 60 > 9 ? (miss % 3600) / 60 + "" : "0" + (miss % 3600) / 60;
        String ss = (miss % 3600) % 60 > 9 ? (miss % 3600) % 60 + "" : "0" + (miss % 3600) % 60;
        return hh + ":" + mm + ":" + ss;
    }

    private void clickRecorder() {
        recordUtils.startRecorder(new RecordUtils.OnRecordListener() {
            @Override
            public void onRecordSuccess() {
            }

            @Override
            public void onRecordFail() {
                Log.i("tan6458", "录音失败请重新录音:");
            }
        });
    }

    private void clickStop() {
        recordUtils.stopRecord(new RecordUtils.OnRecordListener() {
            @Override
            public void onRecordSuccess() {
            }

            @Override
            public void onRecordFail() {
                Log.i("tan6458", "录音失败请重新录音:");
            }
        });
    }

    public void clickPlay() {
        recordUtils.clickPlay(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Toast.makeText(RecordActivity.this, "播放失败", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recordUtils.destroy();
    }
}